######################################################################################
#
# This file was provided as part of the SharePoint Mystery Deployment package
# Created by Christopher Kimbell (http://sharepointmystery.com)
#
######################################################################################


# -----------------------------------------------------------------
# Creates an object used to manipulate an application zone
# -----------------------------------------------------------------
function New-Zone()
{
    $obj=new-object PSObject -Property @{
		Protocol = "http";
		HostNameStart = ""
        Port = 80;
        AuthenticationMethod = "Kerberos";
        AuthenticationProvider = (New-SPAuthenticationProvider -DisableKerberos:$false);
		AllowAnonymousAccess = $true;
		Type = "Default";
		SignInRedirectURL = "";
		Enabled = $false;
		WebApplication = $null;
    }
	# -----------------------------------------------------------------
    # Gets the Absolute Url of the zone
    # -----------------------------------------------------------------
    Add-Property $obj "AbsoluteUrl" `
	{
		switch($this.Type)
		{
			"Custom" { return $this.Protocol + "://" + $this.HostNameStart + $farm.CustomUrlDomain}
			"Intranet" { return $this.Protocol + "://" + $this.HostNameStart + $farm.IntranetUrlDomain}
			"Internet" { return $this.Protocol + "://" + $this.HostNameStart + $farm.InternetUrlDomain}
			"Extranet" { return $this.Protocol + "://" + $this.HostNameStart + $farm.ExtranetUrlDomain}
			default { return $this.Protocol + "://" + $this.HostNameStart + $farm.DefaultUrlDomain }
		}
	} 
	# -----------------------------------------------------------------
    # Gets the Absolute Host header of the zone
    # -----------------------------------------------------------------
    Add-Property $obj "HostHeader" `
	{
		$i = $this.AbsoluteUrl.IndexOf("//");
		return $this.AbsoluteUrl.SubString($i + 2)
	} 
	# -----------------------------------------------------------------
    # Gets the Absolute path of the zone
    # -----------------------------------------------------------------
    Add-Property $obj "AbsolutePath" `
	{
		return $farm.IisRoot + "\" + $this.WebApplication.ApplicationType + "\" + $this.Type 
	} 
	# -----------------------------------------------------------------
    # Gets the server comment for the zone
    # -----------------------------------------------------------------
    Add-Property $obj "ServerComment" `
	{
		return $farm.Prefix + $this.WebApplication.ApplicationType + "_" + $this.Type 
	}
    # -----------------------------------------------------------------
    # Validates the information in the object
    # -----------------------------------------------------------------
    Add-Method $obj "Validate" `
    {
		if($this.Enabled -eq $false)
		{
			Terminate "This $(this.Type) zone has not been enabled"
		}
        Write-Operation "    Zone"  
        Validate-NotEmpty "ServerComment" $this.ServerComment
		Validate-MemberOf "Type" $this.Type "Default","Custom","Intranet","Extranet","Internet"
		Validate-MemberOf "AuthenticationMethod" $this.AuthenticationMethod "Kerberos","NTLM"
		Validate-MemberOf "Protocol" $this.Protocol "http","https"
    }
	# -----------------------------------------------------------------
    # Extends the web application with this zone
    # -----------------------------------------------------------------
    Add-Method $obj "Provision" `
    {
		Terminate("not implemented yet")
	
		$webApp = Get-SPWebApplication $this.WebApplication.DefaultZone.AbsoluteUrl
		$alternateUrl = Get-SPAlternateURL -WebApplication $webApp -Zone $this.Type
		$ssl = ($this.AbsoluteUrl.StartsWith("https://") -eq $true)
			
		if($alternateUrl -eq $null)
		{
			#BUG: This makes changes to the default zone web.config that prevent it from working
			# the whole microsoft.identityModel section is gone; no idea why
			# postponed until I can figure it out
			
			Write-Operation "Creating $($this.Type) zone for $($this.AbsoluteUrl)"
			New-SPWebApplicationExtension -AllowAnonymousAccess:$this.AllowAnonymousAccess `
				-AuthenticationMethod $this.AuthenticationMethod `
				-HostHeader $this.HostHeader `
				-Identity $webApp `
				-Name $this.ServerComment `
				-Path $this.AbsolutePath `
				-Port $this.Port `
				-SecureSocketsLayer:$ssl `
				-Url $this.AbsoluteUrl `
				-Zone $this.Type
				#-SignInRedirectURL $this.SignInRedirectURL `
		}
    }
	
	# -----------------------------------------------------------------
    # Creates the AAM entries to support traditional https -> http
    # -----------------------------------------------------------------
    Add-Method $obj "CreateHttpHttpsMapping" `
    {	
		$webApp = Get-SPWebApplication $this.WebApplication.DefaultZone.AbsoluteUrl
		$localUrl = "http://" + $this.HostNameStart + ".local"
		$localHostName = $this.HostNameStart + ".local"
		
		# the idea here is that locally on the server, everything is HTTP
		# for HTTPS, this is terminated at some NLB such as ISA or UAG
		# from this towards the SharePoint application, a http://***.local address is used
		
		New-SPAlternateUrl -Url $this.AbsoluteUrl -WebApplication $webApp -Zone $this.Type
		New-SPAlternateUrl -Url $localUrl -WebApplication $webApp -Zone $this.Type -Internal	
		
		Add-SPMIisBinding -WebApplication $webApp `
			-Zone "Default" `
			-Protocol "http" `
			-HostName $localHostName `
			-Port "80"
    }
	
    return $obj
}
